package cn.gov.pbc.tims.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.gov.pbc.tims.entity.User;

@Repository("UserDao")
public class UserDao {
	
	@Autowired  
    private SessionFactory sessionFactory;  
  
    private Session getCurrentSession() {  
        return this.sessionFactory.getCurrentSession();  
    }
    
    public User load(Integer id){
    	return (User)getCurrentSession().load(User.class, id);
    }
    
    public User get(Integer id){
    	return (User)getCurrentSession().get(User.class, id);
    }
    
    public List<User> findAll(){
    	Query<User> q = getCurrentSession().createQuery(" from User", User.class).setCacheable(true);
    	List<User> Users = q.getResultList();
    	return Users;
    }
    
    public List<User> find(String hql, Object[] values){
    	Query<User> query = getCurrentSession().createQuery(hql, User.class);
    	if (values != null){
    		for (int i = 0, size = values.length; i < size; i++){
    			query.setParameter(i, values[i]);
    		}
    	}
    	List<User> Users = query.setCacheable(true).getResultList();
    	return Users;
    }
    
    public void persist(User entity){
    	getCurrentSession().persist(entity);
    }
    
    public Integer save(User entity){
    	return (Integer)getCurrentSession().save(entity);
    }
    
    public void saveOrUpdate(User entity){
    	getCurrentSession().saveOrUpdate(entity);
    }
    
    public void delete(User entity){
    	getCurrentSession().delete(entity);
    }
    
    public void flush(){
    	getCurrentSession().flush();
    }

}
